package com.shushan.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.ScreenManager;
import com.shushan.db.ChildBeanDao;
import com.shushan.db.ChildBookDao;
import com.shushan.home.HomeActivity;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.ChildBookBean;
import com.shushan.login.bean.UserBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.setting.BookSelectActivity;
import com.shushan.setting.ChildSettingActivity;
import com.shushan.setting.ParentPswSetActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;
import com.shushan.util.UmengEventUtil;

import java.util.List;

/**
 * @ClassName: InviteCodeActivity
 * @Desciption: 请输入邀请码
 * @author: yichaohua
 * @date: 2018-09-15
 */
public class InviteCodeActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "InviteCodeActivity";

    private ImageView backImg;
    private TextView jumpText;
    private Button confirmButton;
    private EditText codeEdit;

    private UserBean userBean;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userBean = (UserBean) Session.getSession().get("userBean");

        setContentView(R.layout.invite_code_activity);

        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true, this);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        initView();
    }

    private void initView() {
        backImg = findViewById(R.id.actionbar_left_back_img);
        backImg.setOnClickListener(this);
        jumpText = findViewById(R.id.invite_code_jump_tv);
        jumpText.setOnClickListener(this);
        confirmButton = findViewById(R.id.invite_code_confirm_btn);
        confirmButton.setOnClickListener(this);
        codeEdit = findViewById(R.id.invite_code_edit);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.actionbar_left_back_img:
                UmengEventUtil.onEvent(shuShanApplication,"invite_code_ignore_id");
                onBack();
                break;
            case R.id.invite_code_jump_tv:
                UmengEventUtil.onEvent(shuShanApplication,"invite_code_ignore_id");
                onBack();
                break;
            case R.id.invite_code_confirm_btn:
                if(valide()){
                    submit();
                }
                break;
            default:
                break;
        }
    }

    private boolean valide() {
        String code = codeEdit.getText().toString();
        if(TextUtils.isEmpty(code)){
            showToast("请输入邀请码");
            return false;
        }

        return true;
    }

    private void submit() {
        final String code = codeEdit.getText().toString();
        int userId = shuShanApplication.getCurrentUseId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_INVITE_CODE);
        urlParse.putValue("user_id", userId)
                .putValue("invite_code", code);

        showWaitDialog();

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        UmengEventUtil.onEvent(shuShanApplication,"invite_code_success_id",code);

                        onBack();
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private void onBack() {
        if(userBean != null){
            List<ChildBean> childList = userBean.getChildList();
            if(childList.size() > 0){
                new ChildBeanDao(shuShanApplication).dropAndAddList(childList);

                ChildBean childBean = childList.get(0);
                if(ToolUtils.isListEmpty(childBean.getBookBeanList())){
                    Intent intent = new Intent(this,BookSelectActivity.class);
                    intent.putExtra("gradeId",childBean.gradeId);
                    intent.putExtra("semester",childBean.semester);
                    intent.putExtra("firstTime",1);
                    startActivityAllow(intent);

                    new ChildBookDao(shuShanApplication).clearTab();
                }else {
                    List<ChildBookBean> bookBeanList = childBean.getBookBeanList();
                    new ChildBookDao(shuShanApplication).addOrUpdateList(bookBeanList);

                    if(!TextUtils.isEmpty(userBean.password)){
                        Intent intent = new Intent(this,HomeActivity.class);
                        intent.putExtra("firstTime",1);
                        startActivityAllow(intent);
                    }else {
                        Intent intent = new Intent(this,ParentPswSetActivity.class);
                        intent.putExtra("firstTime",1);
                        startActivityAllow(intent);
                    }
                }
            }else {
                new ChildBeanDao(shuShanApplication).clearTab();

                Intent intent = new Intent(this,ChildSettingActivity.class);
                intent.putExtra("firstTime",1);
                startActivityAllow(intent);
            }
        }else {
            Intent intent = new Intent(this,ChildSettingActivity.class);
            intent.putExtra("firstTime",1);
            startActivityAllow(intent);
        }

        onBackAction();
    }
}
